const redlock = require('./redlockConfig');

async function test() {
  let lock;
  let intervalId;
  try {
    lock = await redlock.lock('locks:account:322456', 20000); // Initial lock for 20 seconds
    const extendLockIfNeeded = async () => {
      try {
        const remainingTime = lock.expiration - Date.now();
        if (remainingTime <= 5000) {
          await lock.extend(10000);
          console.log('Lock extended');
        }
      } catch (extendError) {
        console.error('Failed to extend lock:', extendError);
      }
    };

    intervalId = setInterval(extendLockIfNeeded, 5000);
    await new Promise(resolve => setTimeout(resolve, 30000)); // Simulate a 30-second task

  } catch (error) {
    console.error('Failed to acquire lock:', error);
  } finally {
    if (intervalId) {
      clearInterval(intervalId);
    }
    if (lock) {
      try {
        await lock.unlock();
        console.log('Lock released');
      } catch (releaseError) {
        console.error('Failed to release lock:', releaseError);
      }
    }
  }
}

test();